package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.messageManager.MessageManager;
import com.itheima.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MessageController {
    @Autowired
    private MessageManager messageManager;

    // 获取app端的环信账号
    @GetMapping("/huanxin/user")
    public ResponseEntity huanxinUser() {

        // 创建环信vo
        HuanXinVo huanXinVo = new HuanXinVo();
        huanXinVo.setUsername("HX" + UserHolder.get().getId());
        System.out.println("HX" + UserHolder.get().getId());
        huanXinVo.setPassword("123456");

        return ResponseEntity.ok(huanXinVo);
    }
    //回答陌生人问题
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map<String, String> param) {
        // 1.接收参数
        Long jiarenId = Long.parseLong(param.get("userId"));
        String reply = param.get("reply");

        // 2.调用manager
        messageManager.strangerQuestions(jiarenId,reply);
    }
    // 添加好友（联系人）
    @PostMapping("/messages/contacts")
    public void addContacts(@RequestBody Map<String,Long> param){
        // 1.获取好友id
        Long friendId = param.get("userId");
        // 2.调用manager
        messageManager.addContacts(friendId);
    }
    // 查看联系人
    @GetMapping("/messages/contacts")
    public ResponseEntity findContactsByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.findContactsByPage(pageNum, pageSize);
    }
    //查看喜欢列表
    @GetMapping("/messages/loves")
    public ResponseEntity findBycomment(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ){
        return messageManager.findBycomment(pageNum,pageSize,3);
    }
    //查看点赞列表
    @GetMapping("/messages/likes")
    public ResponseEntity findByLike(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ){
        return messageManager.findBycomment(pageNum,pageSize,1);
    }
    //查看评论列表
    @GetMapping("/messages/comments")
    public ResponseEntity findBycomments(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ){
        return messageManager.findBycomment(pageNum,pageSize,2);
    }
    //公告通知
    @GetMapping("/messages/announcements")
    public ResponseEntity findAnnouncements(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ){
        return messageManager.findAnnouncements(pageNum,pageSize);
    }
    //视频上传
    @PostMapping("/smallVideos")
    public void saveVideo(MultipartFile videoThumbnail ,MultipartFile videoFile) throws IOException {
        Long userId = UserHolder.get().getId();
         messageManager.saveVideo(userId,videoThumbnail,videoFile);
    }
}